package com.rqbl.service.impl;


import com.rqbl.entity.Reply;
import com.rqbl.mapper.ReplyMapper;
import com.rqbl.myutils.PageBean;
import com.rqbl.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bruce
 * @since 2024-07-19
 */
@Service
public class ReplyServiceImpl implements ReplyService {

    @Autowired
    private ReplyMapper replyMapper;

    @Override
    public Integer addReply(String text, Integer cid, Integer uid) {
        Reply reply = new Reply();
        reply.setCid(cid);
        reply.setUid(uid);
        reply.setRtext(text);
        replyMapper.addReply(reply);
        return reply.getRid();
    }

    @Override
    public List<Reply> getReplyByCid(Integer cid, Integer page, Integer limit) {
        PageBean pageBean = new PageBean();
        pageBean.setPage(page);
        pageBean.setPageSize(limit);
        pageBean.setTotalSize(replyMapper.getReplyCountByCid(cid));
        int totalPage = pageBean.getTotalSize() / pageBean.getPageSize();
        if(pageBean.getTotalSize() % pageBean.getPageSize() != 0) {
            totalPage++;
        }
        pageBean.setTotalPage(totalPage);
        return replyMapper.getReplyByCidPage((pageBean.getPage()-1)*pageBean.getPageSize(), pageBean.getPageSize(),cid);
    }
}
